package com.yndsoft.jc.supplier.join;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yndsoft.jc.R;
import com.yndsoft.jc.model.ReturnMsg;
import com.yndsoft.jc.supplier.adapter.AddressAdapter;
import com.yndsoft.jc.supplier.model.AddressModel;
import com.yndsoft.jc.supplier.net.util.ProgressSubscriber;
import com.yndsoft.jc.supplier.utils.Constants;
import com.yndsoft.jc.supplier.utils.ToastUtil;
import com.yndsoft.jc.supplier.widget.popupwindow.BasePopupWindow;
import com.yndsoft.jc.supplier.widget.popupwindow.OnChooseAddressListener;
import com.yndsoft.jc.supplier.widget.popupwindow.OnChooseDataListener;
import com.yndsoft.jc.supplier.widget.recyclerview.DividerItemDecoration;

import java.util.List;

public class PopupWindowAddress extends BasePopupWindow implements View.OnClickListener {

    private TextView addressValue;
    private RecyclerView addressList;
    private int provinceKey = 0;
    private String provinceValue = "";
    private int cityKey = 0;
    private String cityValue = "";
    private int districtKey = 0;
    private String districtValue = "";
    private OnChooseDataListener listener;

    public void setOnChooseDataListener(OnChooseDataListener listener) {
        this.listener = listener;
    }

    public PopupWindowAddress(Context context, String address) {
        super(context, address);
    }

    @Override
    public int layoutResId() {
        return R.layout.pop_win_address;
    }

    @Override
    public void init() {
        TextView addressConfirm = findViewById(R.id.address_confirm);
        addressConfirm.setOnClickListener(this);
        addressValue = findViewById(R.id.address_value);
        addressList = findViewById(R.id.address_list);
        if (!TextUtils.isEmpty(mValue)) {
            addressValue.setText(mValue);
        }
        province();
    }

    private void province() {
        mHttpUtil.province(new ProgressSubscriber(mContext) {
            @Override
            public void next(ReturnMsg returnMsg) {
                loadData(returnMsg, Constants.PROVINCE);
            }
        }, Constants.PROVINCE);
    }

    private void city(int pid) {
        data(Constants.CITY, pid);
    }

    private void district(int pid) {
        data(Constants.DISTRICT, pid);
    }

    private void data(final String type, int pid) {
        mHttpUtil.city(new ProgressSubscriber(mContext) {
            @Override
            public void next(ReturnMsg returnMsg) {
                loadData(returnMsg, type);
            }
        }, type, pid);
    }

    private void loadData(ReturnMsg returnMsg, String level) {
        String info = returnMsg.getInfo();
        if (info != null) {
            List<AddressModel> model = new Gson().fromJson(info, new TypeToken<List<AddressModel>>() {}.getType());
            if (model != null && model.size() > 0) {
                addressList.setLayoutManager(new LinearLayoutManager(mContext));
                AddressAdapter addressAdapter = new AddressAdapter(mContext, model, level);
                setListener(addressAdapter);
                addressList.setAdapter(addressAdapter);
                addressList.addItemDecoration(new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL_LIST));
            }
        }
    }

    private void setListener(AddressAdapter addressAdapter) {
        addressAdapter.setOnChooseAddressListener(new OnChooseAddressListener() {
            @Override
            public void onChooseAddress(int key, String value, String level) {

                if (level.equals(Constants.PROVINCE)) {
                    provinceKey = key;
                    provinceValue = value;
                    city(key);
                }
                if (level.equals(Constants.CITY)) {
                    cityKey = key;
                    cityValue = value;
                    district(key);
                }
                if (level.equals(Constants.DISTRICT)) {
                    districtKey = key;
                    districtValue = value;
                }

                addressValue.setText(provinceValue + " " + cityValue + " " + districtValue);
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.address_confirm:
                chooseData();
                break;
        }
    }

    private void chooseData() {
        if (!"".equals(provinceValue) && !"".equals(cityValue) && !"".equals(districtValue)) {
            if (listener != null) {
                listener.onChooseData(provinceKey, provinceValue, cityKey, cityValue, districtKey, districtValue);
                dismiss();
            }
        } else {
            ToastUtil.show(mContext, "请选择省市区！");
        }
    }
}